package com.mmlc.meimei.lichengkongjian.shoppingbag;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.Adapter.PayTypeAdapter;
import com.mmlc.meimei.lichengkongjian.App;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.ShopOrderPayMethodRequestFilter;
import com.mmlc.network.responsebean.ShopOrderPayTypeResponseBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.widget.MyListView;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class PayTypeActivity extends InterActivity {

    @InjectView(R.id.iv_back)
    ImageView ivBack;
    @InjectView(R.id.rl_title)
    RelativeLayout rlTitle;
    @InjectView(R.id.tv_name)
    TextView tvName;
    @InjectView(R.id.tv_money)
    TextView tvMoney;
    @InjectView(R.id.container_list)
    MyListView containerList;
    @InjectView(R.id.activity_pay_type)
    LinearLayout activityPayType;
    @InjectView(R.id.tv_next)
    TextView tvNext;

    @Override
    protected int initContentView() {
        return R.layout.activity_pay_type;
    }

    String market_price;
    @Override
    protected void initView() {
        ButterKnife.inject(this);
        tvName.setText(App.user.getShopName());

        ivBack.setOnClickListener(this);
        tvNext.setOnClickListener(this);

        Intent intent = getIntent();
        market_price = intent.getStringExtra("market_price");
        if (market_price != null) {
            tvMoney.setText(market_price);
        }
        getData();
    }

    ShopOrderPayTypeResponseBean resultBean;
    PayTypeAdapter adapter;

    private void getData() {
        final ShopOrderPayMethodRequestFilter filter = new ShopOrderPayMethodRequestFilter(this);
        filter.requestBean.platform="unline";
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<ShopOrderPayTypeResponseBean>() {

            @Override
            public void onSuccess(ShopOrderPayTypeResponseBean result) {
                resultBean = result;
                if (result != null && result.getData() != null && result.getData().size() > 0) {
                    adapter = new PayTypeAdapter(result.getData(), context);
                    containerList.setAdapter(adapter);
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_next:
                jmpToShorOrderActivity();

                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void jmpToShorOrderActivity() {
        //是否有选中支付方式
        if (adapter == null) {
            ToastUtils.showToast("请选择支付方式");
            return;
        }
        if (adapter.checkBean == null) {
            ToastUtils.showToast("请选择支付方式");
            return;
        }
    /*    String str = "{\n" +
                "    \"code\": 0,\n" +
                "    \"data\": [\n" +
                "        {\n" +
                "            \"description\": \"描述\",\n" +
                "            \"ext_info\": [\n" +
                "                {\n" +
                "                    \"key\": \"sale_no\",\n" +
                "                    \"value\": \"销售单号\"\n" +
                "                }\n" +
                "            ],\n" +
                "            \"img\": \"收银凭证图片样例\",\n" +
                "            \"pay_code\": \"unified_cash\",\n" +
                "            \"pay_name\": \"中央收银\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"img\": \"www.baidu.com\",\n" +
                "    \"message\": \"OK\"\n" +
                "}";
        Gson gson = new Gson();
        ShopOrderPayTypeResponseBean bean = gson.fromJson(str, ShopOrderPayTypeResponseBean.class);*/


        Intent intent = new Intent(this, ShopOrderActivity.class);
        intent.putExtra("pay_name", adapter.checkBean.getPay_name());
        intent.putExtra("pay_code", adapter.checkBean.getPay_code());
        intent.putExtra("shop_Price",market_price);
        intent.putParcelableArrayListExtra("shop_voucher", (ArrayList<? extends Parcelable>) adapter.checkBean.getExt_info());
        intent.putExtra("sample_img",adapter.checkBean.getImg());
        startActivity(intent);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }
}
